package com.chexiehui.jc.widget;

import android.content.Context;
import android.support.annotation.NonNull;

import com.chexiehui.jc.R;
import com.chexiehui.jc.adapter.ChoosePriceGridAdapter;
import com.chexiehui.jc.entity.PriceConditionInfo;
import com.lxj.xpopup.impl.PartShadowPopupView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class CustomChoosePricePopup extends PartShadowPopupView {

    @BindView(R.id.nsgv_price_grid)
    NonSlidingGridView nsgvPriceGrid;
    private Unbinder mUnBinder;
    private PriceConditionInfo choosePrice;
    private List<PriceConditionInfo> priceConditionData;
    private ChoosePriceGridAdapter priceConditionAdapter;
    private ICustomChoosePricePopupListener listener;

    public CustomChoosePricePopup(@NonNull Context context) {
        super(context);
    }


    public CustomChoosePricePopup(@NonNull Context context, List<PriceConditionInfo> list) {
        super(context);
        this.priceConditionData = list;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layput_custom_choose_price_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        mUnBinder = ButterKnife.bind(this);
        if(priceConditionData == null){
            priceConditionData = new ArrayList<>();
            priceConditionData.add(new PriceConditionInfo("价格不限", "", ""));
            priceConditionData.add(new PriceConditionInfo("10万以下", "0", "10"));
            priceConditionData.add(new PriceConditionInfo("10-15万", "10", "15"));
            priceConditionData.add(new PriceConditionInfo("15-20万", "15", "20"));
            priceConditionData.add(new PriceConditionInfo("25-30万", "25", "30"));
            priceConditionData.add(new PriceConditionInfo("30-40万", "30", "40"));
            priceConditionData.add(new PriceConditionInfo("50万以上", "50", ""));
        }
        nsgvPriceGrid.setAdapter(priceConditionAdapter = new ChoosePriceGridAdapter(getContext(), priceConditionData));
        nsgvPriceGrid.setOnItemClickListener((parent, view, position, id) -> {
            if (priceConditionAdapter.getChoosePosition() == position) {
                return;
            }
            priceConditionAdapter.changeState(position);
            dismiss();
            if (listener != null) {
                listener.onChoosePrice(priceConditionData.get(position));
            }
        });
        priceConditionAdapter.changeState(-1);
    }

    public void initPriceModel(PriceConditionInfo choosePrice) {
        this.choosePrice = choosePrice;
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
        setPriceGridInfo();
    }

    public void setListener(ICustomChoosePricePopupListener listener) {
        this.listener = listener;
    }

    private void setPriceGridInfo() {
        if (choosePrice != null) {
            for (int i = 0; i < priceConditionData.size(); i++) {
                if (choosePrice.getName().equals(priceConditionData.get(i).getName())) {
                    priceConditionAdapter.changeState(i);
                    break;
                }
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mUnBinder != null) {
            mUnBinder.unbind();
        }
    }

    public interface ICustomChoosePricePopupListener {
        void onChoosePrice(PriceConditionInfo info);
    }

}
